package com.spring.security.service.mine.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.spring.security.dao.SysPermissionDao;
import com.spring.security.dao.mine.AttentionDataDao;
import com.spring.security.entity.SysPermission;
import com.spring.security.entity.mine.AttentionDataDTO;
import com.spring.security.entity.mine.MyAttentionQryInputDTO;
import com.spring.security.entity.mine.MyAttentionQryOutputDTO;
import com.spring.security.service.mine.AttentionDataService;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 权限表(SysPermission)表服务实现类
 *
 * @author makejava
 * @since 2019-08-29 21:13:29
 */
@Service("attentionDataService")
public class AttentionDataServiceImpl implements AttentionDataService {
    @Resource
    private SysPermissionDao sysPermissionDao;

    @Resource
    private AttentionDataDao attentionDataDao;

    private static final Logger log = LoggerFactory.getLogger(AttentionDataServiceImpl.class);

    /**
     * 新增数据
     * @param attentionDataDTO 实例对象
     * @return 实例对象
     */
    @Override
    public Integer addAttention(AttentionDataDTO attentionDataDTO) {
        attentionDataDTO.setCreatedDate(new Date());
        attentionDataDTO.setUpdateDate(new Date());
        log.info("新增关注表入参：{}",attentionDataDTO);
        int attention = this.attentionDataDao.addAttention(attentionDataDTO);
        log.info("新增关注表结果：{}",attention);
        return attention;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public AttentionDataDTO queryById(Integer id) {
        return this.attentionDataDao.queryById(id);
    }


    /**
     * 查询我的关注列表
     * @param myAttentionQryInputDTO
     * @return 我的关注列表
     */
    @Override
    public MyAttentionQryOutputDTO queryMyAttentionList(MyAttentionQryInputDTO myAttentionQryInputDTO){
        log.info("查询我的关注列表，入参：{}",myAttentionQryInputDTO);
        //准备查询参数
        MyAttentionQryOutputDTO myAttentionQryOutputDTO = new MyAttentionQryOutputDTO();
        int pageNum = Integer.parseInt(myAttentionQryInputDTO.getPageNum());
        int pageSize = Integer.parseInt(myAttentionQryInputDTO.getPageSize());
        //设置分页参数
        PageHelper.startPage(pageNum,pageSize);
        List<AttentionDataDTO> attentionDataDTOList = this.attentionDataDao.queryMyAttentionList(myAttentionQryInputDTO);
        PageInfo pageInfo = new PageInfo(attentionDataDTOList);
        myAttentionQryOutputDTO.setMyAttentionList(pageInfo.getList());
        myAttentionQryOutputDTO.setTotalNum(String.valueOf(pageInfo.getTotal()));
        log.info("查询我的关注列表，结果：{}",myAttentionQryOutputDTO);
        return myAttentionQryOutputDTO;
    }

    /**
     * 修改数据
     *
     * @param attentionDataDTO 实例对象
     * @return 实例对象
     */
    @Override
    public int updateMyAttentionById(AttentionDataDTO attentionDataDTO){
        int updateResult = 0;
        try {
            attentionDataDTO.setUpdateDate(new Date());
            updateResult = this.attentionDataDao.updateMyAttentionById(attentionDataDTO);
        }catch (Exception e){
            log.error("更新我的关注发生异常，异常信息：{}",e);
        }
        return updateResult;
    }

    /**
     * 通过主键删除数据
     *
     * @param attentionDataDTO 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteMyAttentionById(AttentionDataDTO attentionDataDTO) {
        return this.attentionDataDao.deleteMyAttentionById(attentionDataDTO.getId()) > 0;
    }

    /**
     * 查询关注我的列表
     * @param myAttentionQryInputDTO
     * @return 查询关注我的列表
     */
    @Override
    public MyAttentionQryOutputDTO queryAttentionMeList(MyAttentionQryInputDTO myAttentionQryInputDTO){
        log.info("查询我的关注列表，入参：{}",myAttentionQryInputDTO);
        //准备查询参数
        MyAttentionQryOutputDTO myAttentionQryOutputDTO = new MyAttentionQryOutputDTO();
        int pageNum = Integer.parseInt(myAttentionQryInputDTO.getPageNum());
        int pageSize = Integer.parseInt(myAttentionQryInputDTO.getPageSize());
        //设置分页参数
        PageHelper.startPage(pageNum,pageSize);
        List<AttentionDataDTO> attentionDataDTOList = this.attentionDataDao.queryAttentionMeList(myAttentionQryInputDTO);
        PageInfo pageInfo = new PageInfo(attentionDataDTOList);
        myAttentionQryOutputDTO.setMyAttentionList(pageInfo.getList());
        myAttentionQryOutputDTO.setTotalNum(String.valueOf(pageInfo.getTotal()));
        log.info("查询我的关注列表，结果：{}",myAttentionQryOutputDTO);
        return myAttentionQryOutputDTO;
    }

}